package gui.ParkingLotWorker;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import system.SystemMode;
import system.SystemTypes;
import system.VCP;
import vcpClasses.ParkingLot;
import vcpClasses.Worker;

import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JComboBox;

import control.LocalOpertaionController;
import control.LoginController;

/**
 * this class use to Initialize Lot Screen
 * @author G8
 *
 */
public class InitializeLotScreen extends JPanel implements ActionListener{
	public JButton btnLogout;
	public JButton btnBack;
	public JButton btnInitialize;
	private LocalOpertaionController localOpControl;
	private Worker lotWorker;
	private SystemTypes initResult;
	/**
	 *Initialize Lot Screen Constructor 
	 */
	public InitializeLotScreen(){
		super();
		initialize();
	}
	/**
	 * This method contains the view of the Panel
	 */
	private void initialize() {
		localOpControl = new LocalOpertaionController(null);
		setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Initialize Lot", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		setBackground(SystemColor.menu);
		setBounds(VCP.guiX,VCP.guiY,500,400);
		setLayout(null);
		VCP.frame.setBounds(this.getBounds());
		
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(InitializeLotScreen.class.getResource("/img/back.png")));
		btnBack.setSize(150, 30);
		btnBack.setBounds(281, 22, 102, 35);
		btnBack.addActionListener(this);
		add(btnBack);
		
		btnLogout = new JButton("Logout");
		btnLogout.setSize(150, 30);
		btnLogout.setBounds(393, 22, 78, 35);
		btnLogout.addActionListener(this);
		add(btnLogout);
		
		btnInitialize = new JButton("Initialize");
		btnInitialize.addActionListener(this);
		btnInitialize.setBounds(211, 169, 88, 35);
		add(btnInitialize);
		
		JLabel lblPlease = new JLabel("Please press the button to initialize this parking lot.");
		lblPlease.setBounds(80, 144, 367, 14);
		add(lblPlease);
	}
	/**
	 * This method listens to the buttons on the panel
	 * @param e is the buttons the user Click
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnInitialize){
			lotWorker = (Worker)VCP.currentUser;
			System.out.println("the current lot is" + VCP.currentLot.getParkingLotNumber());
			ParkingLot newParkingLot = VCP.currentLot;
			SystemTypes lotToInit = new SystemTypes(VCP.currentLot,null);
			initResult = localOpControl.ExecuteLotInitialization(lotToInit);
			if (initResult.getSysMode()==SystemMode.SYS_ERR){
				JOptionPane.showMessageDialog(null, "Failed to connect the server ! Lot "+newParkingLot.getParkingLotNumber()+" could not be initialized !", "Error",
                        JOptionPane.ERROR_MESSAGE);
			}
			else if (initResult.getSysMode()==SystemMode.FAIL){
				JOptionPane.showMessageDialog(null, "Lot "+newParkingLot.getParkingLotNumber()+" is already initialized !", "Attention",
                        JOptionPane.INFORMATION_MESSAGE);
			}
			else if (initResult.getSysMode()==SystemMode.CONN_ERR){
				JOptionPane.showMessageDialog(null, "Failed to connect the database ! Lot "+newParkingLot.getParkingLotNumber()+" could not be initialized !", "Error",
                        JOptionPane.ERROR_MESSAGE);
			}
			else if (initResult.getSysMode()==SystemMode.OK){
				JOptionPane.showMessageDialog(null, "Lot "+newParkingLot.getParkingLotNumber()+" was successfully initialized !", "Success",
                        JOptionPane.PLAIN_MESSAGE);
			}
		}
		if (e.getSource() == btnBack){
			VCP.setScreen(this, VCP.prevScreen);
			VCP.prevScreen.setBounds(VCP.guiX,VCP.guiY,500,400);
			VCP.prevScreen.setLayout(null);
			VCP.frame.setBounds(VCP.prevScreen.getBounds());
		}
		if (e.getSource() == btnLogout){
			SystemTypes userToLogout = new SystemTypes(VCP.currentUser,null);
			boolean logoutRes = LoginController.LogOut(userToLogout);
			if (logoutRes)
				VCP.setGUI(0, this);
			else{
				JOptionPane.showMessageDialog(null, "Failed to logout!", "Error",
	                    JOptionPane.ERROR_MESSAGE);
			}
		}
	}

}
